package com.fesine.crm.service.impl;

import com.fesine.crm.dto.SysUserDto;
import com.fesine.crm.po.EmployeeInfoPo;
import com.fesine.crm.po.SysUserPo;
import com.fesine.crm.service.SysUserService;
import com.fesine.service.impl.BaseServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @description: 类描述
 * @author: Fesine
 * @createTime:2017/10/12 14:41
 * @update:修改内容
 * @author: Fesine
 * @updateTime:2017/10/12 14:41
 */
@Service
public class SysUserServiceImpl extends BaseServiceImpl<SysUserPo> implements SysUserService {

    @Override
    public int save(SysUserPo po) {
        //录入公司号字母大写
        po.setPacteraNo(po.getPacteraNo().toUpperCase());
        return daoService.insert(po);
    }

    @Override
    public int update(SysUserPo po) {
        setUpperPacteraNo(po);

        return daoService.update(po);
    }

    @Override
    public int delete(SysUserPo po) {
        //录入公司号字母大写
        setUpperPacteraNo(po);
        return daoService.delete(po);
    }

    @Override
    public SysUserPo get(SysUserPo po) {
        //录入公司号字母大写
        setUpperPacteraNo(po);
        return daoService.selectOne(po);
    }

    @Override
    public List<SysUserDto> listDto(SysUserPo po) {
        //录入公司号字母大写
        setUpperPacteraNo(po);
        List<SysUserDto> list = new ArrayList<>();
        List<SysUserPo> poList = daoService.selectList(po);
        SysUserDto dto;
        EmployeeInfoPo emp;
        for (SysUserPo userPo : poList) {
            dto = new SysUserDto();
            emp = new EmployeeInfoPo();
            emp.setPacteraNo(userPo.getPacteraNo());
            emp = daoService.selectOne(emp);
            if (emp == null) {
                continue;
            }
            BeanUtils.copyProperties(userPo, dto);
            dto.setName(emp.getName());
            list.add(dto);
        }
        return list;
    }

    private void setUpperPacteraNo(SysUserPo po) {
        //录入公司号字母大写
        if (StringUtils.isNotBlank(po.getPacteraNo())) {
            po.setPacteraNo(po.getPacteraNo().toUpperCase());
        }
    }
}
